package com.gotop.system.service.impl.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gotop.common.base.BaseServiceImpl;
import com.gotop.entity.system.Post;
import com.gotop.mapper.system.PostDao;
import com.gotop.system.service.system.IPostService;
import com.gotop.vo.system.PostVO;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: cem
 * @description:
 * @author: superfish
 * @create: 2020-12-25 13:51
 **/
@Service
public class PostServiceImpl extends BaseServiceImpl<PostDao, Post> implements IPostService {

    @Override
    public IPage<PostVO> selectPostPage(IPage<PostVO> page, PostVO post) {
        return page.setRecords(baseMapper.selectPostPage(page, post));
    }

    @Override
    public String getPostIds(String tenantId, String postNames) {
        final String[] split = postNames.split(",");
        List<Post> postList = baseMapper.selectList(Wrappers.<Post>query().lambda().eq(Post::getTenantId, tenantId).in(Post::getPostName, split));
        if (postList != null && postList.size() > 0) {
            return postList.stream().map(post -> String.valueOf(post.getId())).distinct().collect(Collectors.joining(","));
        }
        return null;
    }

    @Override
    public List<String> getPostNames(String postIds) {
        final String[] split = postIds.split(",");
        List<Long> longs = new LinkedList<>();
        for (String s : split) {
            longs.add(Long.parseLong(s));
        }
        return baseMapper.getPostNames(longs);
    }

}
